define(
    ["PDUtilDir/dataService", "PDUtilDir/grid", "AppPathDir/fleet/orderForVisitorInfos/addCar", "AppPathDir/fleet/orderForVisitorInfos/choiceDriver", "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar", "PDUtilDir/inputSelect", "Date", "DateCN", "css!DateCss"],
    function (DataService, Grid, AddCarSupport, ChoiceDriverSupport, Util, Tool, Slidebar, inputSelect) {
        function addSupportInit($scope, $compile) {
            return new addSupport($scope, $compile);
        };

        function addSupport($scope, $compile) {
            this.$scope = $scope;
            this.$compile = $compile;
        };
        addSupport.fn = addSupport.prototype;

        addSupport.fn.init = function () {
            var _this = this;

            _this.entity = {
                cars: new Array()
            };
            _this.bind = {};
            _this.cache = {};
        };

        addSupport.fn.digest = function () {
            this.$scope.$digest();
        };

        addSupport.fn.run = function (id) {
            var _this = this;
            _this.init(id);

            this.slidebars = Slidebar({
                size: 'lg',
                url: getServer()
                + "/static/app/fleet/orderForVisitorInfos/views/add.html",
                $scope: this.$scope,
                $compile: this.$compile,
                afterLoad: function ($Panel) {
                    _this.initialization();
                }
            });
        };
        addSupport.fn.initialization = function () {
            this.baseInit();
            this.carInfoInit();
        };
        addSupport.fn.baseInit = function () {
            var _this = this;
            //车辆类型
            inputSelect.dropCache();
            inputSelect({
                id: "typeName",
                key: {
                    id: "dictCode",
                    name: "dictName",
                    data: "data",
                },
                data: _this.$scope.cache.type,
                simpleData: true,
                searchAble: false,
                callback: function (id) {
                    _this.bind.carType = id[0];
                    _this.$scope.addOrderForm.carType.$setViewValue(_this.bind.carType);
                }
            });
            //用车日期
            $("#useDate").datetimepicker({
                //设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
                language : "cn",
                //输出格式化
                format : 'yyyy-mm-dd hh:ii:ss',
                //直接选择‘今天’
                todayBtn : true,
                //设置最精确的时间选择视图
                minView : 'hour',
                //高亮当天日期
                todayHighlight : true,
                //显示“上午”“下午”
                showMeridian : true,
                //选择完毕后自动关闭
                autoclose : true
            });
            //还车日期
            $("#returnDate").datetimepicker({
                //设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
                language : "cn",
                //输出格式化
                format : 'yyyy-mm-dd hh:ii:ss',
                //直接选择‘今天’
                todayBtn : true,
                //设置最精确的时间选择视图
                minView : 'hour',
                //高亮当天日期
                todayHighlight : true,
                //显示“上午”“下午”
                showMeridian : true,
                //选择完毕后自动关闭
                autoclose : true
            });
        };

        /* 添加车辆 */
        addSupport.fn.addCarSupportFun = function (){
            if(this.addCarSupport)
                return this.addCarSupport;
            return this.addCarSupport = AddCarSupport(this.$scope, this.$compile)
        };

        /* 选择司机 */
        addSupport.fn.choiceDriverSupportFun = function (){
            console.info(this.choiceDriverSupport);

            if(this.choiceDriverSupport)
                return this.choiceDriverSupport;
            return this.choiceDriverSupport = ChoiceDriverSupport(this.$scope, this.$compile)
        };

        // 刷新车辆列表
        addSupport.fn.reloadCarsSupport = function(){
            var _this = this;
            return {
                run : function() {
                    var selecteds = new Array(),
                        datas = DataService.getObject('car.team.search.cars');

                    $.each(datas, function(index, row) {
                        selecteds.push($.extend(row, {
                            uuid: new Date().getTime(),
                            carId: row.id
                        }));
                    });
                    _this.entity.cars = selecteds;
                    _this.gird.reload({
                        data: _this.entity.cars
                    });
                }
            }
        };
        // 刷新车辆司机
        addSupport.fn.reloadDriverSupport = function(){
            var _this = this;
            return {
                run : function() {
                    var selecteds = _this.entity.cars,
                        driver = DataService.getObject('car.team.search.driver');

                    $.each(selecteds, function(index, row) {
                        if (row.uuid == driver.uuid){
                            row = $.extend(row, driver, {
                                driverId: driver.id
                            });
                        }
                    });
                    _this.gird.reload();
                }
            };
        };
        // 删除车辆
        addSupport.fn.removeCarSupport = function(){
            var _this = this;
            return {
                run : function(uuid) {
                    var selecteds = _this.entity.cars;

                    $.each(selecteds, function(index, row) {
                        if (row.uuid == uuid){
                            selecteds.splice(index, 1);
                            return;
                        }
                    });
                    _this.gird.reload();
                }
            };
        };
        addSupport.fn.save = function() {
            var _this = this;
            $.ajax({
                url : getServer() + "/flea/hclv/car/agency/add.htmls",
                data : {
                    'model' : JSON.stringify(_this.entity)
                },
                success : function(data) {
                    if (data.status != 200) {
                        return;
                    }
                    _this.$scope.grid.refresh();
                    Util.alert(data.message);
                    _this.slidebars.close();
                }
            });
        };
        addSupport.fn.carInfoInit = function () {
            var _this = this;

            _this.gird = Grid({
                id: "CarTeamOrderDetailGirdDivId",
                pageSize: 10,
                index: true,
                multi: false,
                pagination: true,
                cache: false,
                layout: [{
                    name: "车牌号",
                    field: "plateNumber",
                    sort: true
                }, {
                    name: "座位数",
                    field: "seatNumber",
                    width: '50px'
                }, {
                    name: "司机",
                    field: "name",
                    sort: true
                }, {
                    name: "电话",
                    field: "phone"
                }, {
                    name: "性别",
                    field: "sex",
                    width: '50px'
                }, {
                    name: "年龄",
                    field: "age",
                    width: '50px'
                }, {
                    name: "设置",
                    field: "id",
                    width: '100px',
                    format: function (e) {
                        return '<a href="javascript:angular.element(\'#OrderForVisitorGirdDivId\').scope().addSupport.removeCarSupport().run(\'' + e.row.uuid + '\')">删除</a>'
                            + '<a href="javascript:angular.element(\'#OrderForVisitorGirdDivId\').scope().addSupport.choiceDriverSupportFun().run(\'' + e.row.uuid + '\')">选择司机</a>';
                    }
                }],
                toolbar: [
                    {
                        name: "添加",
                        icon: "fa fa-search",
                        callback: function (event) {
                            _this.addCarSupportFun().run();
                        }
                    }
                ],
                data: this.entity.cars
            });
        };
        return addSupportInit;
    });